package org.dromara.common.core.wx;

import com.google.common.collect.Maps;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class WxMpConfig {

    /**
     * 设置微信公众号的appid
     */
    @Value("${wx.mp.appid}")
    private String appId;

    /**
     * 设置微信公众号的app secret
     */
    @Value("${wx.mp.secret}")
    private String secret;

    /**
     * 设置微信公众号的token
     */
    @Value("${wx.mp.token}")
    private String token;

    /**
     * 设置微信公众号的EncodingAESKey
     */
    @Value("${wx.mp.aesKey}")
    private String aesKey;

    @Bean("wxMpService")
    public WxMpService wxMpService() {
        WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
        wxMpDefaultConfig.setAppId(appId);
        wxMpDefaultConfig.setSecret(secret);
        wxMpDefaultConfig.setToken(token);
        wxMpDefaultConfig.setAesKey(aesKey);
        Map<String, WxMpConfigStorage> configMap = Maps.newHashMap();
        configMap.put(appId,wxMpDefaultConfig);
        WxMpService service = new WxMpServiceImpl();
        service.setMultiConfigStorages(configMap);
        return service;
    }
}
